import { getConstructionPageList, deleteContract, exportData  } from "@/views/wy-operate/api/repair.js";
import { mapGetters } from "vuex"
import departmentDialog from './components/dialog'
import { downloadBlob } from '@/utils'

export default {
    components: {
        departmentDialog,
    },
    provide() {
		return {
			initList: this.queryData,
		};
    },
    mounted() {
        this.queryData();
    },
    data() {
        return {
            // 表格列表数据
            tableData: [],
            // 请求参数
            params: {
                current: 1,
                size: 20,
                queryString:'',
                businessType:''
            },
            // 选中的数据
            selectDataArr: [],
            // 分页
            pager: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20 // 每页显示多少条
            },
            // 页面loading
            loading: false,
            // 类型筛选 
            businessTypes:[
                { value: '', label: '全部'},
                { value: '1', label: '工程施工合同'}, 
                { value: '2', label: '物业服务合同'}
            ],
        }
    },
    computed: {
        ...mapGetters(['permissions']),
    },
    methods: {
        // 搜索监听
        searchByKey(val) {
            this.queryData({queryString: val, current: 1});
        },
        // 数据请求
        queryData(params = {}) {
            this.loading = true
            let requestData = {...this.params, ...params}
            getConstructionPageList(requestData).then(res => {
                const { current, size, total, records } = res.data.data
                this.tableData = records
                this.params = requestData
                this.pager = { 
                    total,
                    currentPage: current, 
                    pageSize: size 
                }
            }).finally(() => {
                this.loading = false
            })
        },
        // 合同类型改变监听
        handleBusinessTypeChange(businessType){
            this.queryData({current: 1, businessType })
        },
        // 选中监听
        selectionChange(val) {
            this.selectDataArr = val;
        },
        // 数据删除
        deleteData() {
            if (this.selectDataArr.length == 0) {
                this.$alert("请勾选要删除的数据！");
                return;
            }
            let ids = this.selectDataArr.map((sd) => sd.contractId);
            this.$confirm("此操作将永久删除该数据, 是否继续?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                deleteContract(ids).then((rep) => {
                    this.$message.success("删除成功!");
                    this.queryData({current: 1});
                }).catch((e) => {
                    this.$message.fail("删除失败!");
                });
            }).catch(() => {
                this.$message.info("已取消删除");
            });
        },
        //表格按钮点击事件监听
        handleBtnClick(type, row = {}){
            switch (type) {
                case 'preview':
                case 'edit':
                    this.$refs.editDialog.show({
                        type,
                        data: JSON.parse(JSON.stringify(row))
                    })
                    break;
                case 'delete':
                    this.$confirm("此操作将永久删除该数据, 是否继续?", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        deleteContract([row.contractId]).then((rep) => {
                            this.$message.success("删除成功!");
                            this.queryData({current: 1});
                        }).catch((e) => {
                            this.$message.fail("删除失败!")
                        });
                    }).catch(() => {
                        this.$message.info("已取消删除");
                    });
                    break;
                case 'upload':
                    break;
                default:
                    break;
            }
        },
        // 添加
        toAdd() {
            this.$refs.editDialog.show({
                type: 'add'
            })
        },
        // 修改单页数据条数
        changePageSize(size) {
            this.queryData({current:1, size})
        },
        // 翻页
        changeCurrentPage(current) {
            this.queryData({current})
        },
        // 导出
        async exportData(){
            const loading = this.$loading({
                lock: true,
                text: "数据处理中",
            });
            const response = await exportData('contract',this.tableData.map(item => item.contractId))
            loading.close()
            var time = new Date();
            let name = time.getFullYear()+'-'+ (time.getMonth()+1) +'-' +  time.getDate() +' ' + time.getHours()+":" + time.getMinutes()+":"+ time.getSeconds() + '-'+ '合同录入.xls';
            downloadBlob(response.data, name)
        }
    },
};
